package com.cbwl.eoas.common.framework.session;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 
 * <p>[描述信息：session的基础管理]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月19日 下午6:50:41
 */
public final class SessionManager {
	
	public static final String PARAMETER_NAME = "securityUser";
	
	public static final String GOTO_URL = "gotoUrl";

	/**
	 * <p>判断是否登录</p>
	 * 
	 * @return
	 * @author: yuyunbo 
	 * @date: Created on 2014-4-26 下午03:59:03
	 */
	public static boolean isAuthenticated() {
		Subject user = SecurityUtils.getSubject();
		return user.isAuthenticated();
	}

	/**
	 * <p>清理当前登录人的session</p>
	 * 
	 * @author: yuyunbo 
	 * @date: Created on 2014-4-26 下午04:02:39
	 */
	public static void cleanUserSession() {
		Subject user = SecurityUtils.getSubject();
		user.getSession(true).removeAttribute(PARAMETER_NAME);
	}

	public static SecurityUserDTO getCurrentUser() {
		Subject user = SecurityUtils.getSubject();
	return (SecurityUserDTO) user.getSession(true).getAttribute(PARAMETER_NAME);
	}
	
	/**
	 * 
	 * <p>获取当前sesionId</p>
	 * 
	 * @return
	 * @author: 虞云波 - yuyunbo@lawxp.com
	 * @date: Created on 2016-4-25 下午08:09:00
	 */
	public static String getCurrentSessionId(){
		return  SecurityUtils.getSubject().getSession(true).getId().toString();
	}
	

	/**
	 * <p>向session中插入数据</p>
	 * 
	 * @param parameterName
	 * @param obj
	 * @author: yuyunbo 
	 * @date: Created on 2014-4-26 下午04:03:59
	 */
	public static void setAttribute(final String parameterName, final Object obj) {
		Subject user = SecurityUtils.getSubject();
		user.getSession(true).setAttribute(parameterName, obj);
	}

	/**
	 * <p>通过变量名从session中取出数据</p>
	 * 
	 * @param parameterName
	 * @author: yuyunbo 
	 * @date: Created on 2014-4-26 下午04:03:59
	 */
	public static Object getAttribute(final String parameterName) {
		Subject user = SecurityUtils.getSubject();
		return user.getSession(true).getAttribute(parameterName);
	}

	/**
	 * <p>通过变量名删除数据</p>
	 * 
	 * @param session
	 * @param parameterName
	 * @author: yuyunbo 
	 * @date: Created on 2014-4-26 下午04:05:12
	 */
	public static void removeAttribute(final String parameterName) {
		Subject user = SecurityUtils.getSubject();
		user.getSession(true).removeAttribute(parameterName);
	}
}
